import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path';
import viteCompression from 'vite-plugin-compression';
import removeConsole from "vite-plugin-remove-console";
import { visualizer } from "rollup-plugin-visualizer";
import autoprefixer from "autoprefixer";
import postCssPxToRem from "postcss-pxtorem";

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
  return {
    plugins: [
      react(),
      viteCompression({
        threshold: 1024 * 10,
      }),
      removeConsole(),
      visualizer({
        // 打包完成后自动打开浏览器，显示产物体积报告
        open: true,
      }),
    ],
    // base: "/test",
    server: {
      force: true,
      port: 3033,  
      host: '0.0.0.0',  
      hot: true, // 开启热更新 
      proxy: { // 代理配置
        "/h5_api": {
          // target: "http://172.29.1.54:8085",
          // target: "http://172.29.0.231:8085",
          target: "http://119.12.161.188:8085",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/h5_api/, ''),
        }
      }
    },
    resolve: {
      alias: {
        // eslint-disable-next-line no-undef
        '@': path.resolve(__dirname, './src')
      }
    },
    css: {  
      preprocessorOptions: {  
        scss: {  
          additionalData: `@import "src/styles/variables.less";`  
        }  
      },
      postcss: {
        plugins: [
          //自动补充前缀
          autoprefixer({
            overrideBrowserslist: [
              "Android 4.1",
              "iOS 7.1",
              "Chrome > 31",
              "ff > 31",
              "ie >= 8",
            ],
          }),
          //单位转化
          // postCssPxToRem({
          //   // rootValue: 75, // 75表示750设计稿，37.5表示375设计稿
          //   // //@ts-expect-error 这里不做类型检测
          //   // rootValue({ file }) {
          //   //   return file.indexOf('antd-mobile') !== -1 ? 37.5 : 75
          //   // },
          //   rootValue: 37.5,
          //   propList: ["*"], // 需要转换的属性，这里选择全部都进行转换
          //   selectorBlackList: ["norem"], // 过滤掉norem-开头的class，不进行rem转换
          // }),
        ],
      },
    },
  }
})
